//
//  HomeViewController.m
//  QuickCure37
//
//  Created by 马文星 on 16/6/17.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "HomeViewController.h"
#import "MLTopView.h"
#import "CycleScrollView.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "UIView+Frame.h"
#import "MLMenuView.h"
#import "CaseDetailViewController.h"
#import "ProvienceViewController.h"
#import "WeatherDTO.h"
#import "PublicWelfareController.h"


@interface HomeViewController ()

/** 顶部 天气 View */
@property (nonatomic, strong) MLTopView *topView;

/** 轮播器 View */
@property (nonatomic, strong) UIView *bannerView;
@property (nonatomic, strong) CycleScrollView *cycleScorllView;

/** 中间的`名医通`三个字 */
@property (nonatomic, strong) UIView *midView;

/** 九宫格 View */
@property (nonatomic, strong) UIView *bottomView;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
    [self loadWeatherData:@"上海"];
    
//    CaseDetailViewController *cdVC = [[CaseDetailViewController alloc] init];
//    NSArray *titles = @[@"肿瘤",@"心血管",@"神经科",@"血液科",@"骨科",@"公益"];
//    
//    cdVC.caseDetailStyle = 0;
//    cdVC.typeString = titles[0];
//    
//    [self.navigationController pushViewController:cdVC animated:YES];
    
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [self.cycleScorllView.animationTimer resumeTimer];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];

    [self.cycleScorllView.animationTimer pauseTimer];
}

/** 根据城市 获取天气情况 */
- (void)loadWeatherData:(NSString *)city{
    
    WEAKSELF
    [[NetWorkManager sharedManager] loadWeatherWithCurrentCity:city successBlock:^(NSDictionary *cityDic) {
        
        weakSelf.topView.weatherDTO = [WeatherDTO dtoWithDic:cityDic];
        
        [weakSelf setWeatherButton:city];
        
    } errorBlock:^(NSString *error) {
      
#warning  **************【 需要打开的 】**********************

//        [SVPAlertManager alertErrorStatus:error];
    }];
    
}

- (void)setWeatherButton:(NSString *)city{

    //加下划线
    
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithString:city];
    NSDictionary *dic = @{
                          NSFontAttributeName:FONT(16),
                          NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle],
                          NSForegroundColorAttributeName:kThemeColor
                          };
    [content addAttributes:dic range:NSMakeRange(0, [content length])];
    [self.topView.weatherAddressBtn setAttributedTitle:content forState:UIControlStateNormal];
}


- (void)setupUI {
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = kThemeColor;

    self.title = @"马医一生";
    
    /*!
     *  添加控件
     */
    [self.view addSubview:self.topView];
    [self.view addSubview:self.bannerView];
    [self.view addSubview:self.midView];
    [self.view addSubview:self.bottomView];
    
    /*!
     *  约束控件
     */
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(64);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(80);
    }];
    
    [self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.topView.mas_bottom);
        make.height.mas_equalTo(200);
        make.left.right.mas_equalTo(0);
    }];
    
    [self.midView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.bannerView.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(60);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.midView.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        
    }];
    
    
    /*!
     *  设置子控件的布局
     */
    [self.view layoutIfNeeded];
    
    //configureScrollView 方法一定要在layoutIfNeeded 后面，不然的话
    //是得不到bannerView的fram值的
    [self configureScrollView];
    
    [self create9Views];

}

/** 创建九宫格 */
- (void)create9Views{

    CGFloat W = SCREEN_WIDTH/3;
    CGFloat H = self.bottomView.height/2;
    CGFloat padding = 1;
    
    NSArray *titles = @[@"肿瘤",@"心血管",@"神经科",@"血液科",@"骨科",@"公益"];
    
    NSArray *imgaesNames = @[@"zl",@"xyg",@"sj",@"xy",@"gt",@"gy"];
    
    for (int i = 0; i < 6; ++i) {
        
        MLMenuView *menuView = [[MLMenuView alloc] init];
        menuView.backgroundColor = [UIColor whiteColor];
        
        int col = i%3; //x
        int row = i/3; //y
        
        CGFloat X = padding + col * (padding + W);
        CGFloat Y = padding + row * (padding + H);
    
        menuView.frame = CGRectMake(X, Y, W, H);
        
        [self.bottomView addSubview:menuView];
        
        menuView.menuIconView.image = [UIImage imageNamed:imgaesNames[i]];
        menuView.menuLab.text = titles[i];
        menuView.tag = 100+i;
        
        menuView.userInteractionEnabled = Y;
        
        UITapGestureRecognizer *singleTap =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickMenu:)];
        [menuView addGestureRecognizer:singleTap];
        
    }
}

/** 配置轮播期 */
- (void)configureScrollView{
    
    //url数组
    NSArray *imageArray = @[
                            @"http://img.guahao.com/portal_upload/rollingnews/1453447883150.jpg",
                            @"http://img.guahao.com/portal_upload/mediafocus/1461826474435.jpg",
                            @"http://img.guahao.com/portal_upload/rollingnews/1461574027543.jpg"
                            ];
    
    //数组 保存imageView
    NSMutableArray *imageViewArray = [NSMutableArray arrayWithCapacity:3];
    
    for (int i = 0; i < imageArray.count; ++i) {
        
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,  self.bannerView.frame.size.height)];
        
        [imageV sd_setImageWithURL:[NSURL URLWithString:imageArray[i]] placeholderImage:[UIImage imageNamed:@"good_image_default"] options:SDWebImageRetryFailed];
        
        [imageViewArray addObject:imageV];
    }
    
    CycleScrollView *cycleScorllView = [[CycleScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.bannerView.frame.size.height) animationDuration:2.0];
    
    self.cycleScorllView = cycleScorllView;
    
    [self.bannerView addSubview:cycleScorllView];
    
    cycleScorllView.scrollView.showsHorizontalScrollIndicator = NO;
    
    cycleScorllView.totalPagesCount = ^NSInteger(void){
        return imageViewArray.count;
    };
    cycleScorllView.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex){
        
        return imageViewArray[pageIndex];
    };
    
    
    //因为原型图没有给出点击做什么事，但是呢，我们还是要留下接口的，因为一般的轮播器点击都会有交互的
    
    cycleScorllView.TapActionBlock = ^(NSInteger pageIndex){
        
        loggerWarn(@"点了%ld张",pageIndex);
    };
}


#pragma mark  - Actions
- (void)clickMenu:(UITapGestureRecognizer *)tap{
    
    int tag = ((int)tap.view.tag-100);
    
    if (tag == 5) {
        
        PublicWelfareController *pubVC = [[PublicWelfareController alloc] init];
        
        [self.navigationController pushViewController:pubVC animated:YES];
        
    }else{
    
        CaseDetailViewController *cdVC = [[CaseDetailViewController alloc] init];
        NSArray *titles = @[@"肿瘤",@"心血管",@"神经科",@"血液科",@"骨科"];
        
        cdVC.caseDetailStyle = tag;
        cdVC.typeString = titles[tag];
        
        [self.navigationController pushViewController:cdVC animated:YES];
    }
    

}

#pragma mark  - Lazy
/** 顶部 天气 View */
- (MLTopView *)topView{
    if (_topView == nil) {
        _topView = [[MLTopView alloc] init];
        _topView.backgroundColor = RGBCOLOR(170, 240, 220);
        
        __weak typeof(self) weakSelf = self;
        _topView.clickWeatherAddress = ^(){
            
            ProvienceViewController *provienceVC = [[ProvienceViewController alloc] init];
            
            provienceVC.clickCityBlock = ^(NSString *city){
                
                //重新请求数据
                [weakSelf loadWeatherData:city];
                
            };
            
            [weakSelf.navigationController pushViewController:provienceVC animated:YES];
        };
        
    }
    return _topView;
}

/** 轮播器 View */
- (UIView *)bannerView{
    if (_bannerView == nil) {
        _bannerView = [[UIView alloc] init];
    }
    return _bannerView;
}

/** 中间的`名医通`三个字 */
- (UIView *)midView{
    if (_midView == nil) {
        _midView = [[UIView alloc] init];
        
        //左边的VIew
        UIView *smlV = [[UIView alloc] init];
        smlV.backgroundColor = kThemeColor;
        [_midView addSubview:smlV];
        smlV.layer.cornerRadius = 6;
        smlV.layer.masksToBounds = YES;
        
        [smlV mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(15);
            make.top.mas_equalTo(15);
            make.bottom.mas_equalTo(-15);
            make.width.mas_equalTo(12);
            
        }];
        
        //右边的文字
        UILabel *lab = [[UILabel alloc] init];
        [_midView addSubview:lab];
        lab.text = @"名医通";
        lab.textAlignment = NSTextAlignmentCenter;
        lab.textColor = [UIColor blackColor];
        lab.font = FONT(20);
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerY.mas_equalTo(_midView.mas_centerY);
            make.left.mas_equalTo(smlV.mas_right).offset(15);
        }];
        
    }
    return _midView;
}

/** 九宫格 View */
- (UIView *)bottomView{
    if (_bottomView == nil) {
        _bottomView = [[UIView alloc] init];
         _bottomView.backgroundColor = [UIColor lightGrayColor];
        
     }
    return _bottomView;
}







@end
